import { defineStore } from 'pinia'
import { login } from '@/api/login'
import { getToken, setToken } from '@/utils/auth'

export const useUserStore = defineStore('user', {
  state: () => {
    return {
      token: getToken(),
      id: '',
      name: ''
    }
  },

  actions: {
    // 登录
    login(userInfo) {
      const userName = userInfo.userName.trim()
      const password = userInfo.password
      return new Promise((resolve, reject) => {
        login(userName, password).then(res => {
          setToken(res.data.accessToken)
          this.token = res.data.accessToken
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
  }
})